$(document).ready(function(){
    
    $("#share-link").click(function(){
        $(".imageThumb").hide();
        $(".link-content").hide();
        $(".sub-section-main").show();
        $("#postLink1").val("");
    });
    $('textarea').focus(function(){
        $(this).css('color', '#333');
        if ($(this).val() == $(this).context.defaultValue){
            $(this).val("");
        }
        $(this).parent().find("span.spanSubmitComment").css('display', 'block');
    }).blur(function(){
        if ($(this).val() == "" || $(this).val() == "Bạn đang nghĩ gì?"){
            $(this).val($(this).context.defaultValue);
            $(this).css('color', '#838383');
            $(this).parent().find("span.spanSubmitComment").hide();
        }
        
    }); 
    
    $('#commentLink').focus(function (){
        $('#commentLink').prettyComments({
            maxHeight: 500
        });
    });
    $('#commentEmbed').focus(function (){
        $('#commentEmbed').prettyComments({
            maxHeight: 500
        });
    });
    $('.commentCon').focus(function (){
        $(this).prettyComments({
            maxHeight: 500
        });
    });
    $('#comment').focus(function (){
        $(this).prettyComments({
            maxHeight: 500
        });
    });
    $('#btnBlogComment').click(function (){
        var comment = $('#comment').val();
        if(comment!='Bạn đang nghĩ gì?' && trim(comment)!=''){
            var data = "";
            data = data+'&share_for='+share_for;
            data = data+'&mid_view='+mid_view;
            data = data+'&group_id='+group_id;
            data = data+'&comment='+comment;
            $.ajax({
                type: "POST",
                dataType: 'json',
                url: "/process.php",
                data: "action=blog_comment"+data,
                success: function(msg){
                    if(msg.status=='ok'){
                        $('#displayBlogComment').prepend(msg.html);
                        $('#comment').val('');
                        $('#comment').blur();
                        $('#comment').height(16);
                        $('textarea.commentCon').focus(function(){
                            $(this).css('color', '#333');
                            if ($(this).val() == $(this).context.defaultValue){
                                $(this).val("");
                                
                            }
                            $(this).parent().find("span.spanSubmitComment").css('display', 'block');
                        }).blur(function(){
                            if ($(this).val() == ""){
                                
                                $(this).val($(this).context.defaultValue);
                                $(this).css('color', '#838383');
                                $(this).parent().find("span.spanSubmitComment").hide();
                            }

                        });
                         
                    }else if(msg.status=='not_login'){
                        alert('Bạn phải đăng nhập trước khi bình luận!');
                    }
                }
            });
        }
    });
    $('#attach_link1').click(function (){
        var link = $('#postLink1').val();
        if(trim(link)!=''){
            var data = "";
            data = data+'&link='+link;
            $.ajax({
                type: "POST",
                dataType: 'json',
                url: "/process.php",
                data: "action=attach_link1"+data,
                beforeSend: function(){
                    $('#attach_link1').hide();
                    $('#imgLoadingAttachLink1').show();
                    $('#btnBlogShareLink1').hide();
                },
                success: function(msg){
                    $('#imgLoadingAttachLink1').hide();
                    $('#attach_link1').show();
                    if(msg.status=='ok'){
                        $('#btnBlogShareLink1').show();
                        if(typeof(msg.data.title)!="undefined"){
                            $('#linkTitle').html(msg.data.title);
                        }
                        if(typeof(msg.data.metaTags)!="undefined"){
                            $('#linkDescription').html(msg.data.metaTags);
                        }
                         
                        if(typeof(msg.data.images)!="undefined"){
                            
                            var $htmImg="";
                            var numImg=0;
                            $.each(msg.data.images, function(index, value) { 
                                index=parseInt(index+1);
                                if(index==1)
                                {
                                    $htmImg+='<img id="'+index+'" src="'+value+'" style="width: 100px; display: inline;">';
                                }else
                                {
                                    $htmImg+='<img id="'+index+'" src="'+value+'" style="width: 100px;">';
                                }
                                numImg++;
                            });
                         
                            $('#image-link-thumb').html($htmImg);
                            $('#totalimg').html(numImg);
                            $('#positioncurimg').html(1);
                            $('.imageThumb').show();
                            $('.link-content').show();
                            $('.link-content').css("width","390px");
                            if(numImg<=0)
                            {
                                $('.thumb-controll').hide();
                                $('.imageThumb').hide();
                                $('.link-content').css("width","auto");
                            }
                            else if(numImg==1)
                            {
                                $('.thumb-controll').hide();
                            }
                            else
                            {
                                $('.thumb-controll').show();
                                $('.imageThumb').show();
                                $('.link-content').css("width","390px");
                            }
                            $(".sub-section-main").hide();
                        }
                    }else{
                        $('#btnBlogShareLink1').hide();
                    }
                }
            });
        }
    });
   
    $('#btnBlogShareLink1').click(function (){
        var link = $('#postLink1').val();
        if(strpos(link, '&')){
            link = str_replace('&', '#####', link);
        }
        var title = $('#linkTitle').html();
        var description = $('#linkDescription').html();
        var comment = $('#commentLink').val();
        var imgsrc=$("#image-link-thumb img:visible").attr("src");
        if(typeof(imgsrc)=="undefined")
            imgsrc="";
        if(trim(link)!='' && trim(title)!=''){
            var data = "";
            data = data+'&share_for='+share_for;
            data = data+'&mid_view='+mid_view;
            data = data+'&group_id='+group_id;
            data = data+'&link='+link;
            data = data+'&title='+title;
            data = data+'&description='+description;
            data = data+'&comment='+comment;
            data = data+'&imgsrc='+imgsrc;
            $.ajax({
                type: "POST",
                dataType: 'json',
                url: "/process.php",
                data: "action=share_link1"+data,
                beforeSend: function(){
                    $('#btnBlogShareLink1').hide();
                    $('#imgLoadingShareLink1').show();
                },
                success: function(msg){
                    $('#imgLoadingShareLink1').hide();
                    $('#btnBlogShareLink1').hide();
                    if(msg.status=='ok'){
                        $('#postLink1').val('');
                        $('#linkTitle').html('').hide();
                        $('#linkDescription').html('').hide();
                        $('#commentLink').val('');
                        $('#commentLink').blur();
                        $('#commentLink').height("16px");
                        $('#displayBlogComment').prepend(msg.html);
                        $('#share-status').click();
                        $('textarea.commentCon').focus(function(){
                            $(this).css('color', '#333');
                            if ($(this).val() == $(this).context.defaultValue){
                                $(this).val("");
                                $(this).height("16px");
                            }
                            $(this).prettyComments({
                                maxHeight: 500
                            });
                            $(this).parent().find("span.spanSubmitComment").css('display', 'block');
                        }).blur(function(){
                            if ($(this).val() == "" || $(this).val() == "Bạn đang nghĩ gì?"){
                                $(this).height("16px");
                                $(this).val($(this).context.defaultValue);
                                $(this).css('color', '#838383');
                                $(this).parent().find("span.spanSubmitComment").hide();
                            }
        
                        });
                    }else if(msg.status=='not_login'){
                        alert('Bạn phải đăng nhập trước khi gửi liên kết!');
                    }
                }
            });
        }
    });
    $('#btnBlogShareEmbed').click(function (){
        var embed = $('#postEmbed').val();
        var comment = $('#commentEmbed').val();
        if(strpos(embed, '&')){
            embed = str_replace('&', '#####', embed);
        }
        if(trim(embed)!=''){
            var data = "";
            data = data+'&share_for='+share_for;
            data = data+'&mid_view='+mid_view;
            data = data+'&group_id='+group_id;
            data = data+'&embed='+embed;
            data = data+'&comment='+comment;
            $.ajax({
                type: "POST",
                dataType: 'json',
                url: "/process.php",
                data: "action=share_embed"+data,
                beforeSend: function(){
                    $('#btnBlogShareEmbed').hide();
                    $('#imgLoadingShareEmbed').show();
                },
                success: function(msg){
                    $('#imgLoadingShareEmbed').hide();
                    if(msg.status=='ok'){
                        $('#postEmbed').val('');
                        $('#commentEmbed').val('');
                        $('#commentEmbed').blur();
                        $('#btnBlogShareEmbed').show();
                        $('#displayBlogComment').prepend(msg.html);
                        $('#commentEmbed').height("16px");
                        $('#share-status').click();
                        $('textarea.commentCon').focus(function(){
                            $(this).css('color', '#333');
                            if ($(this).val() == $(this).context.defaultValue){
                                $(this).val("");
                                $(this).height("16px");
                            }
                            $(this).prettyComments({
                                maxHeight: 500
                            });
                            $(this).parent().find("span.spanSubmitComment").css('display', 'block');
                        }).blur(function(){
                            if ($(this).val() == "" || $(this).val() == "Bạn đang nghĩ gì?"){
                                $(this).val($(this).context.defaultValue);
                                $(this).css('color', '#838383');
                                $(this).parent().find("span.spanSubmitComment").hide();
                                $(this).height("16px");
                            }
        
                        });
                    }else if(msg.status=='not_login'){
                        alert('Bạn phải đăng nhập trước khi gửi liên kết!');
                    }
                }
            });
        }
    });
    
    $('#aBlogLoadMore').click(function (){
       
        $('.btnmore').find("#waiting").show();
        var $btnmore=$(this);
        $btnmore.hide();
        var view = share_for;
        if(view=='group'){
            var group_id = $('#group_id').val();
        }
        var last_id = $('#last_id').val();
        if(last_id!='end'){
            var mid = mid_view;
            var data = "";
            data = data+'&last_id='+last_id;
            data = data+'&mid='+mid;
            data = data+'&view='+view;
            if(view=='group'){
                data = data+'&group_id='+group_id;
            }
            $.ajax({
                type: "POST",
                dataType: 'json',
                url: "/process.php",
                data: "action=load_more"+data,
                success: function(msg){
                    if(msg.status=='ok'){
                        $('#displayBlogComment').append(msg.html);
                        $('#last_id').val(msg.last_id);
                
                        $('textarea.commentCon').focus(function(){
                            $(this).css('color', '#333');
                            if ($(this).val() == $(this).context.defaultValue){
                                $(this).val("");
                            }
                            $(this).prettyComments({
                                maxHeight: 500
                            });
                            $(this).parent().find("span.spanSubmitComment").css('display', 'block');
                        }).blur(function(){
                            if ($(this).val() == "" || $(this).val() == "Bạn đang nghĩ gì?"){
                                $(this).val($(this).context.defaultValue);
                                $(this).css('color', '#838383');
                                $(this).parent().find("span.spanSubmitComment").hide();
                            }
        
                        }); 
                        $btnmore.show();
                    }else if(msg.status=='end'){
                        $('#last_id').val('end');
                        $('.btnmore').hide();
                        $btnmore.show();
                    }
                    $('.btnmore').find("#waiting").hide();
                    
                }
            });
        }
        
    });
    
    $('#activitiesHome').scroll(function(){
        if  ($('#displayActivities').height()-$('#activitiesHome').scrollTop() <= $(this).height()+10){
            var lastActivityId = $('#lastActivityId').val();
            if(lastActivityId!='end'){
                var data = "";
                data = data+'&lastActivityId='+lastActivityId;
                $.ajax({
                    type: "POST",
                    dataType: 'json',
                    url: "/process.php",
                    data: "action=load_more_activities"+data,
                    success: function(msg){
                        if(msg.status=='ok'){
                            $('#lastActivityId').val(msg.last_id);
                            if(msg.html!=''){
                                $('#displayActivities').append(msg.html);
                            }
                        }
                    }
                });
            }
        }
    }); 
    
    
    // next image
    $('.nextPre .next').click(function(){

        var curr = parseInt($('#positioncurimg').html());
         
        var total = $('#totalimg').html();
        
        if(curr<total)
        {
            var next=parseInt(curr)+1;
            $('#positioncurimg').html(next);
            $('#image-link-thumb #'+curr).hide();
            $('#image-link-thumb #'+next).show();
        }
        
    });	
    // prev image
    $('.nextPre .prev').click(function(){
 
        var curr = $('#positioncurimg').html();
       
        if(curr>1)
        {
            var pre=parseInt(curr)-1;
            $('#positioncurimg').html(pre);
            $('#image-link-thumb #'+curr).hide();
            $('#image-link-thumb #'+pre).show();
        }
 
    });	
    $('#no-thump').click(function(){
        if($(this).is(':checked'))
        {
            $('#image-link-thumb').hide();
            $('.nextPre').hide();
        }else
        {
            $('#image-link-thumb').show();
            $('.nextPre').show();
        }
    });	
    $("#control_dis_stream .sub").hover(function(){
        $(this).parent().find("ul").show();   
               
    },function(){
        $(this).parent().find("ul").hide();   
    });
    
});

function submitChildComment(parent_id){
    var comment = $('#commentCon_'+parent_id).val();
    if(comment!='Bạn đang nghĩ gì?'){
        var data = "";
        data = data+'&comment='+comment;
        data = data+'&parent_id='+parent_id;
        $.ajax({
            type: "POST",
            dataType: 'json',
            url: "/process.php",
            data: "action=blog_comment"+data,
            success: function(msg){
                if(msg.status=='ok'){
                    $('#divListSubComment_'+parent_id+' div.divSubBlogComment').last().before(msg.html);
                    $('#commentCon_'+parent_id).val('Viết bình luận...');
                    $('#commentCon_'+parent_id).css("height","16px");
                    $('#commentCon_'+parent_id).css('color', '#838383');
                    $('#commentCon_'+parent_id).blur();
                    $('#commentCon_'+parent_id).parent().find("span.spanSubmitComment").hide();
                }else if(msg.status=='not_login'){
                    alert('Bạn phải đăng nhập trước khi bình luận!');
                }
            }
        });
    }
}
function setFocusComment(parent_id){
    $('#commentCon_'+parent_id).parent().show();
    $('#commentCon_'+parent_id).focus();    
}

